<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('实验室查询')"/>

</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="device-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">实验室名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="labName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="user-main">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>

</body>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增实验室
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
</script>

<script type="text/html" id="status-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="正常|冻结"
           lay-filter="user-enable" {{d.status=='0' ? 'checked' : '' }}>
</script>


<script type="text/html" id="status-return">
    {{#if (d.requisition == 0) { }}
    <a class="pear-btn pear-btn-success pear-btn-sm" style="cursor:default"> 未借用 </a>
    {{# }else if(d.requisition == 1){ }}
    <a class="pear-btn pear-btn-danger pear-btn-sm" style="cursor:default"> 借用中</a>
    {{# } }}
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let MODULE_PATH = "/lab/manage/";

        let cols = [
            [
                {title: '实验室', field: 'labName', event: 'setcilck'},
                {title: '状态', field: 'status', templet: '#status-enable'},
                {title: '借用状态', templet: '#status-return', event: 'setcilck'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#user-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('switch(user-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "enable";
            } else {
                operate = "disable";
            }
            let loading = layer.load()
            $.ajax({
                url: MODULE_PATH + operate,
                data: JSON.stringify({id: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'setcilck') {
                layer.open({
                    type: 2,
                    title: '查看实验室',
                    shade: 0.1,
                    area: ['800px', '600px'],
                    content: MODULE_PATH + 'view?id=' + obj.data['id']
                });
                //标注选中样式
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '添加实验室',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }

        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        form.on('submit(user-query)', function (data) {
            window.refresh(data.field);
            return false;
        });
        window.refresh = function (param) {
            table.reload('user-table', {where: param});
        }
    })
</script>
</html>